/**
 * Created by isoft-work 5 on 12/5/2014.
 */
$(document).ready(function(){
//   render
   renderSetupEmail();
//   event
   bindEventSetupEmail();
});

function renderSetupEmail() {
   getInfo();
}

function getInfo() {
   ISS.BACKEND.request({
      url: '../../service/admin/setupEmail/getInfo',
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         mapEmailToView(responseInfo);
      }
   });
}

function bindEventSetupEmail() {
   ISS.EVENT.bindEvent('#setup_email_Add', 'click', function () {
      var emailInfo = {};
      mapViewToEmail(emailInfo);
      updateEmail(emailInfo);

   });
}

function updateEmail(emailInfo) {
   ISS.BACKEND.request({
      url: '../../service/admin/setupEmail/updateInfo',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(emailInfo),
      success: function (data) {
         var responseInfo = data.responseObject;
         mapEmailToView(responseInfo);
      }
   })
}

function mapViewToEmail(emailInfo) {
   emailInfo.email = ISS.DOM.getVal('#email_setup_email');
   emailInfo.pass = ISS.DOM.getVal('#pass_setup_email');
   emailInfo.host = ISS.DOM.getVal('#host_setup_email');
   emailInfo.port = ISS.DOM.getVal('#post_setup_email');
   emailInfo.protocol = ISS.DOM.getVal('#protocol_setup_email');
}

function mapEmailToView(emailInfo) {
   ISS.DOM.setVal('#email_setup_email', emailInfo.email);
   ISS.DOM.setVal('#pass_setup_email', emailInfo.pass);
   ISS.DOM.setVal('#host_setup_email', emailInfo.host);
   ISS.DOM.setVal('#post_setup_email', emailInfo.port);
   ISS.DOM.setVal('#protocol_setup_email', emailInfo.protocol);
}
